<?php
if(!isset($_SESSION)) 
{
	 session_start();
}

//初始化用户信息
$_COOKIE['admin_id'] = '';
$_COOKIE['admin_prov'] = '';
$_COOKIE['admin_name'] = '';
$_COOKIE['admin_group'] = '';

class admin_class_admin{
	public $admin_id;
	public $admin_prov;
	public $admin_group;
	public $admin_name;
	
	public function __construct() 
	{
		$this->showmessage = pf_load::init('admin_class_showmessage');
		self::check_admin();
		
		self::check_priv();
		/*pc_base::load_app_func('global','admin');
		if (!module_exists(ROUTE_M)) showmessage(L('module_not_exists'));
		self::manage_log();
		self::check_ip();
		self::lock_screen();
		self::check_hash();
		*/
	}
	
	/**
	 * 判断用户是否已经登陆
	 */
	final public function check_admin() 
	{
		if (!isset($_SESSION['admin']) || !isset($_COOKIE['userinfo']))
		{
			$this->showmessage->html('登录已超时或还没有登录!', pf_controller::set_url('admin', 'login'));
			exit;
		}
		$admin_info = explode('|', $_SESSION['admin']);
		$check_md5 = isset($admin_info[2])?$admin_info[2]:'';
		$_COOKIE['admin_id'] = isset($admin_info[0])?$admin_info[0]:0;
		$_COOKIE['admin_prov'] = isset($admin_info[1])?$admin_info[1]:'';
		if (md5($_COOKIE['userinfo']) !== $check_md5 || $this->admin_id === 0 || $this->admin_prov === '')
		{
			$this->showmessage->html('登录已超时或还没有登录!', pf_controller::set_url('admin', 'login'));
			exit;
		}
		$userinfo = explode('|', $_COOKIE['userinfo']);
		$_COOKIE['admin_name'] = $userinfo[0];
		$_COOKIE['admin_group'] = $userinfo[1];
	}

	
	/**
	 * 权限判断
	 */
	final public function check_priv() 
	{
		$action = isset($_COOKIE['action'])?$_COOKIE['action']:'';
		$controller = isset($_COOKIE['controller'])?$_COOKIE['controller']:'';
		$action = pf_request::route_a();
		$controller = pf_request::route_c();
		$model = pf_request::route_m();
		if ($action === '' || $controller === '')
		{
			$this->showmessage->html('您没有操作权限!');
			exit;
		}

		if(in_array($action, array('check_login', 'index')) && in_array($controller, array('login','index'))) return true;
		

		//获取用户权限
		$access_prov = isset($_COOKIE['admin_prov'])?$_COOKIE['admin_prov']:'';
		$access_prov = explode(',', $access_prov);

		//加载功能菜单
		$menu_obj = pf_load::init('admin_model_sysmenu');
		$sys_menu = $menu_obj->get_sysmenu_list('line', $access_prov);
		//var_export($sys_menu);
		foreach ($sys_menu as $key=>$val)
		{
			if ($val['model'] === $model && $val['controller'] === $controller && $val['action'] === $action)
			{
				return '';
				break;
			}
		}

		$this->showmessage->html('您没有操作权限!');
		exit;

	}

	/**
	 * 
	 * 后台IP禁止判断 ...
	 */
	final private function check_ip()
	{
		$this->ipbanned = pc_base::load_model('ipbanned_model');
		$this->ipbanned->check_ip();
 	}

	/**
	 * 
	 * 记录日志 
	 */
	final private function manage_log() 
	{
		//判断是否记录
		$setconfig = pc_base::load_config('system');
		extract($setconfig);
 		if($admin_log==1){
 			$action = ROUTE_A;
			if($action == '' || strpos($action,'public') || $action == 'init') {
				return false;
			}
	  		$ip = ip();
			$log = pc_base::load_model('log_model');
			$username = param::get_cookie('admin_username');
			$userid = isset($_SESSION['userid']) ? $_SESSION['userid'] : '';
			$time = date('Y-m-d H-i-s',SYS_TIME);
			$url = '?m='.ROUTE_M.'&c='.ROUTE_C.'&a='.ROUTE_A;
			$log->insert(array('module'=>ROUTE_M,'username'=>$username,'userid'=>$userid,'action'=>ROUTE_C, 'querystring'=>$url,'time'=>$time,'ip'=>$ip));
 		}
	}
 	/**
 	 * 检查锁屏状态
 	 */
	final private function lock_screen() {
		if(isset($_SESSION['lock_screen']) && $_SESSION['lock_screen']==1) {
			if(preg_match('/^public_/', ROUTE_A) || (ROUTE_M == 'content' && ROUTE_C == 'create_html') || (ROUTE_M == 'release') || (ROUTE_A == 'login') || (ROUTE_M == 'search' && ROUTE_C == 'search_admin' && ROUTE_A=='createindex')) return true;
			showmessage(L('admin_login'),'?m=admin&c=index&a=login');
		}
	}

	/**
 	 * 检查hash值，验证用户数据安全性
 	 */
	final private function check_hash() {
		if(preg_match('/^public_/', ROUTE_A) || ROUTE_M =='admin' && ROUTE_C =='index' || in_array(ROUTE_A, array('login'))) {
			return true;
		}
		if(isset($_GET['pc_hash']) && $_SESSION['pc_hash'] != '' && ($_SESSION['pc_hash'] == $_GET['pc_hash'])) {
			return true;
		} elseif(isset($_POST['pc_hash']) && $_SESSION['pc_hash'] != '' && ($_SESSION['pc_hash'] == $_POST['pc_hash'])) {
			return true;
		} else {
			showmessage(L('hash_check_false'),HTTP_REFERER);
		}
	}
}